package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by ZhaoJun on 2021/11/24 12:07
 */
@Service
@Transactional//由于在 saveWithFlavor 方法中，进行了两次数据库的保存操作，操作了两张表，那么为了保证数据的一致性，我们需要在方法上加上注解
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

//    @Autowired
//    private DishMapper dishMapper;
    @Autowired
    DishFlavorService dishFlavorService;

    @Override
    public void saveWtihFlavor(DishDto dishDto) {
        //保存菜品的基本信息，disDto继承了dish，传过来的json中包含dish的属性信息
        this.save(dishDto);
        Long dishId = dishDto.getId();
        //保存菜品口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
//        for (DishFlavor flavor : flavors) {
//            flavor.setDishId(dishId);
//        }
        flavors.stream().forEach(item->item.setDishId(dishId));
        dishFlavorService.saveBatch(flavors);


    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();

        BeanUtils.copyProperties(dish,dishDto);

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,id);

        List<DishFlavor> flavors = dishFlavorService.list(lqw);

        dishDto.setFlavors(flavors);

        return dishDto;

    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {

        this.updateById(dishDto);

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(lqw);

        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors.stream().forEach((item)->{item.setDishId(dishDto.getId());});

        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void delByIdsWithFlavors(Long[] ids) {
        ArrayList<Long> list = new ArrayList<>();
        for (Long id : ids) {
            list.add(id);
        }
        //批量删除菜品
        this.removeByIds(list);

        //批量删除菜品id对应的DishFlavor表信息
        for (Long id : ids) {
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,id);
        dishFlavorService.remove(lqw);
        }

        return;


    }

    @Override
    public void status(Integer status, Long[] ids) {
        ArrayList<Dish> list = new ArrayList<>();

        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            list.add(dish);
        }
        this.updateBatchById(list);
    }
}
